/**********************************************************************
* <pre>
* FILE : ServiceActivityManageSummaryDealerSearchDao.java
* CLASS : ServiceActivityManageSummaryDealerSearchDao
*
* AUTHOR : PGM
*
* FUNCTION :  服务活动管理---服务活动评估经销商查询
*
*
*======================================================================
* CHANGE HISTORY LOG
*----------------------------------------------------------------------
* MOD. NO.| DATE     | NAME | REASON | CHANGE REQ.
*----------------------------------------------------------------------
*         |2010-06-13| PGM  | Created |
* DESCRIPTION:
* </pre>
***********************************************************************/
/**
 * $Id: ServiceActivityManageSummaryDealerSearchDao.java,v 1.1 2010/08/16 01:42:18 yuch Exp $
 */
package com.infodms.dms.dao.claim.serviceActivity;
import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;

import com.infodms.dms.bean.TtAsActivityBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TtAsActivityEvaluatePO;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;
/**
 * Function       :  服务活动管理---服务活动评估经销商查询
 * @author        :  PGM
 * CreateDate     :  2010-06-13
 * @version       :  0.1
 */
@SuppressWarnings("unchecked")
public class ServiceActivityManageSummaryDealerSearchDao extends BaseDao{
	public static Logger logger = Logger.getLogger(ServiceActivityManageSummaryDealerSearchDao.class);
	private static final ServiceActivityManageSummaryDealerSearchDao dao = new ServiceActivityManageSummaryDealerSearchDao ();
	public  static final ServiceActivityManageSummaryDealerSearchDao getInstance() {
		return dao;
	}

	/**
	 * Function         : 服务活动管理---服务活动评估经销商查询
	 * @param           : 活动编号
	 * @param           : 活动开始日期
	 * @param           : 活动结束日期
	 * @param           : 当前页码
	 * @param           : 每页显示记录数
	 * @return          : 满足条件的服务活动管理---服务活动评估经销商信息，包含分页信息
	 * @throws          : Exception
	 * LastUpdate       : 服务活动管理
	 */
	public  PageResult<Map<String, Object>>  getServiceActivityManageSummaryDealerSearchQuery(TtAsActivityBean ActivityBean, int curPage,int pageSize) throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append("	   select b.ACTIVITY_ID, b.ACTIVITY_CODE,  b.ACTIVITY_NAME,  c.DEALER_NAME,  r.REGION_NAME,  a.IN_AMOUNT,  to_char(b.STARTDATE,'yyyy-MM-dd')as STARTDATE,  to_char(b.ENDDATE,'yyyy-MM-dd')as ENDDATE \n");
		sql.append("       from TT_AS_ACTIVITY_EVALUATE a,  tt_as_activity  b, tm_dealer  c, tm_region r  \n");
		sql.append("      WHERE a.activity_id = b.activity_id AND  c.dealer_id = a.dealer_id AND c.province_id = r.region_id(+)  AND b.STATUS='"+Constant.SERVICEACTIVITY_STATUS_02+"'  AND  b.IS_DEL='"+Constant.IS_DEL_00+"' and c.dealer_id="+ActivityBean.getDealerId()+" \n");//活动状态为：已经发布; 删除标志为：逻辑未删除;
		if(!"".equals(ActivityBean.getCompanyId())&&null!=ActivityBean.getCompanyId()){//公司ID不为空
			sql.append("		AND b.COMPANY_ID = "+ActivityBean.getCompanyId()+"  \n");
		}
		if(!"".equals(ActivityBean.getActivityCode())&&null!=ActivityBean.getActivityCode()){//活动编号不为空
			sql.append("		AND b.ACTIVITY_CODE like '%"+ActivityBean.getActivityCode()+"%'  \n");
		}
		if(!"".equals(ActivityBean.getStartdate())&&!(null==ActivityBean.getStartdate())){      //活动开始日期不为空
			sql.append("		AND b.STARTDATE >=to_date('"+ActivityBean.getStartdate()+" 00:00:00', 'yyyy-MM-dd HH24:mi:ss') \n");
		}
		if(!"".equals(ActivityBean.getEnddate())&&!(null==ActivityBean.getEnddate())){         //活动结束日期不为空
			sql.append("	    AND b.ENDDATE  <= to_date('"+ActivityBean.getEnddate()+" 23:59:59', 'yyyy-MM-dd HH24:mi:ss') \n");
		}
		sql.append("            ORDER BY b.ACTIVITY_ID DESC  \n");
		PageResult<Map<String, Object>> ps = (PageResult<Map<String, Object>>) pageQuery(sql.toString(), null,  getFunName(), pageSize, curPage);
		return ps;
	}

	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * Function         : 服务活动管理---服务活动评估经销商查询[明细]查询方法
	 * @return          : 满足条件的服务活动管理信息
	 * @throws          : 
	 * LastUpdate       : 2010-06-13
	 */
	public TtAsActivityEvaluatePO serviceActivitySummaryQuery(String activityId){
		List<Object> params = new LinkedList();
		StringBuffer sql = new StringBuffer();
		sql.append("	SELECT A.ACTIVITY_NAME,  B.IN_AMOUNT, B.IN_GUARANTEE, B.OUT_GUARANTEE,  B.REPAIR_AMOUNT, B.MAINTAIN_AMOUNT,  B.ON_AMOUNT,  B.ON_CAMOUNT,B.EVALUATE,  B.MEASURES	,org.org_name	\n");//已经发布
		sql.append("	FROM  tm_dealer dealer, tm_dealer_org_relation org_relation, tm_org org,TT_AS_ACTIVITY A 	\n");
		sql.append("	LEFT OUTER JOIN TT_AS_ACTIVITY_EVALUATE B ON A.ACTIVITY_ID = B.ACTIVITY_ID	\n");
		sql.append("	WHERE A.STATUS="+Constant.SERVICEACTIVITY_STATUS_02+" AND A.IS_DEL="+Constant.IS_DEL_00+" 	\n");
		sql.append("    AND  dealer.dealer_id = org_relation.dealer_id    \n");
		sql.append("    AND  org_relation.org_id = org.org_id     \n");
		sql.append("    AND dealer.dealer_id = B.Dealer_Id     \n");
		if (activityId!=null&&!("").equals(activityId)){
			sql.append(" AND A.ACTIVITY_ID = ?  \n");
			params.add(activityId);
			}
		TtAsActivityEvaluatePO evaluatePO=new TtAsActivityEvaluatePO();
		List list = dao.select(TtAsActivityEvaluatePO.class, sql.toString(), params);
			if (list!=null){
				if (list.size()>0) {
					evaluatePO = (TtAsActivityEvaluatePO) list.get(0);
				}
			}
        return evaluatePO;
	}
}